﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DLL.Entities;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Timers;
using DLL;

namespace RAD
{

    public partial class Form1 : Form
    {
        Radar info = new Radar();
        public Form1()
        {
            InitializeComponent();

            
            TextReader reader = null;

            try
            {
                reader = new StreamReader("../../XML/Config.xml");
                XmlSerializer serializer = new XmlSerializer(typeof(Radar));
                info = (Radar)serializer.Deserialize(reader);

                reader.Close();
                txtbxModel.Text = Convert.ToString(info.Model);
                txtbxVersion.Text = Convert.ToString(info.Version);
            //    txtbxStatus.Text = Convert.ToString(info.RadarStatus);


            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


        private void btnRandom_Click_1(object sender, EventArgs e)
        {
            //Call the random Number method - min and max values
            int returnValue = Utility.RandomNumber(1, 500);
            txtbxDistance.Text = Convert.ToString(returnValue);
            txtbxAltitude.Text = Utility.RandomNumber(1, 13501).ToString();
            txtbxSize.Text = Utility.RandomNumber(1, 7001).ToString();
            txtbxSpeed.Text = Utility.RandomNumber(1, 501).ToString();
            txtbxDirection.Text = Utility.RandomNumber(1, 361).ToString();
            string[] headingArray = { "N", "NE", "NW", "S", "SE", "SW", "E", "W" };
            int headingIndex = Utility.RandomNumber(0, 7);
            txtbxDirection.Text = headingArray[headingIndex];

            radPlane.Enabled = false;
            radMissile.Enabled = false;
            radSpear.Enabled = false;
            radTrident.Enabled = false;
            radFireBall.Enabled = false;



            if (Convert.ToInt16(txtbxSpeed.Text) >= 400)
            {
                radMissile.Select();
                radMissile.Enabled = true;
            }

            else if (Convert.ToInt16(txtbxAltitude.Text) <= 100 && (Convert.ToInt16(txtbxSpeed.Text) <= 500))
            {
                radSpear.Select();
                radSpear.Enabled = true;
            }

            else if (Convert.ToInt16(txtbxAltitude.Text) >= 45 && (Convert.ToInt16(txtbxSpeed.Text) < 100))
            {
                radFireBall.Select();
                radFireBall.Enabled = true;
            }

            else if (Convert.ToInt16(txtbxAltitude.Text) >= 101 && (Convert.ToInt16(txtbxSpeed.Text) < 400))
            {
                radPlane.Select();
                radPlane.Enabled = true;
            }
            else if (Convert.ToInt16(txtbxAltitude.Text) >= 101 && (Convert.ToInt16(txtbxSpeed.Text) < 400))
            {
                radTrident.Select();
                radTrident.Enabled = true;
            }

            btnLockOn.Enabled = true;
        }

      





        public void btnLockOn_Click_1(object sender, EventArgs e)
        {
            Target target = new Target();

            if (radFireBall.Checked)
                target.TargetType = "Fire Ball";
            else if (radMissile.Checked)
                target.TargetType = "Missile";
            else if (radSpear.Checked)
                target.TargetType = "Spear";
            else if (radPlane.Checked)
                target.TargetType = "Plane";
            else if (radTrident.Checked)
                target.TargetType = "Trident";

            target.Altitude = Convert.ToInt16(txtbxAltitude.Text);
            target.Speed = Convert.ToInt16(txtbxSpeed.Text);
            target.Direction = txtbxDirection.Text;
            target.Size = Convert.ToInt16(txtbxSize.Text);
            target.Distance = txtbxDistance.Text;

            XmlSerializer serializer = new XmlSerializer(typeof(Target)); //You can replace Bogey with any type you are trying to serialize 
            XMLHandler.WriteXML(target, "../../../RAD/XML/RadEvent.xml", serializer); //You can replace "Bogey_Serialzed.xml" with the file name needed

            txtbxAltitude.Clear();
            txtbxDirection.Clear();
            txtbxDistance.Clear();
            txtbxSize.Clear();
            txtbxSpeed.Clear();

            //try
            //{

            //    XmlSerializer serializer = new XmlSerializer(typeof(Target));
            //    TextWriter textWriter = new StreamWriter("RadEvent.xml");
            //    serializer.Serialize(textWriter, target);
            //    textWriter.Close();

            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(Convert.ToString(ex));
            //}
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //Radar status = new Radar();

            info.RadarStatus = Convert.ToString(DateTime.Now);

            XmlSerializer serializer = new XmlSerializer(typeof(Radar)); //You can replace Bogey with any type you are trying to serialize 
            XMLHandler.WriteXML(info, "../../../RAD/XML/Config.xml", serializer); //You can replace "Bogey_Serialzed.xml" with the file name needed

            //try
            //{

            //    XmlSerializer serializer = new XmlSerializer(typeof(Radar));
            //    TextWriter textWriter = new StreamWriter("../../../RAD/XML/Config.xml");
            //    serializer.Serialize(textWriter, status);
            //    textWriter.Close();

            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(Convert.ToString(ex));
            //}
        }

      
      
     
        

    }
}
